// pages/chargeOff/chargeOff.js
import api from "../../api/index";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //#region 设置搜索框
    searchList: [
      {
        name: "shop_id",
        type: "topShop",
        offRefresh: true,
      },
      {
        name: "topDate",
        names: ["time_form", "start_date", "end_date"],
        type: "topDate",
      },
      {
        name: "search",
        type: "input",
        label: "会员姓名、手机号",
        span: 16,
      },
      {
        name: "coach_id",
        type: "pickerCoach",
        label: "教练",
        span: 8,
      },
      {
        name: "source",
        type: "picker",
        label: "来源",
        span: 7,
        branch: "redeem",
      },
      {
        name: "is_to",
        type: "picker",
        label: "同步金额",
        span: 7,
      },
      {
        name: "finance",
        names: ["finance_form", "junior_form"],
        type: "multistepPicker",
        label: "类型",
        span: 10,
      },
      {
        type: "money",
        names: ["start_cost", "end_cost"],
        label: "金额",
        span: 16,
      },
      {
        name: "audit",
        type: "picker",
        label: "审核状态",
        span: 8,
      },
    ],
    searchNum: [
      {
        label: "全部",
        show: true,
        num: "0.00",
        subhead: "0笔",
      },
      {
        label: "会员卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "课程",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "周卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "月卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "季卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "年卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "体验卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
      {
        label: "其他卡",
        num: "0.00",
        show: true,
        subhead: "0笔",
      },
    ],
    sExplain: {
      title: "搜索说明",
      content:
        "时间可以选择，对会员名称、手机号、教练、来源、同步金额、类型、金额范围、审核状态进行搜索",
    },

    nExplain: {
      title: "统计说明",
      content:
        "根据搜索条件对总核销订单、会员核销订单、课程核销订单、周卡核销订单、月卡核销订单、季卡核销订单、年卡核销订单、体验卡核销订单、其他卡核销订单进行统计",
    },
    searchParam: {
      time_form: "month",
    },
    //#endregion

    //#region 列表
    list: [],
    isMore: true,
    page: 1,
    isLoading: false,
    //#endregion

    colors: ["#EE8E3A", "#73F0EA"],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      list: [],
      isMore: true,
      page: 1,
      "searchParam.shop_id": wx.getStorageSync("shopID"),
    });
    this.getDataList();
  },
  searchChonge(e) {
    this.setData({
      searchParam: e.detail,
      list: [],
      isMore: true,
      page: 1,
    });
    this.getDataList();
  },
  getDataList() {
    let { page, list, searchParam, searchNum } = this.data;
    searchParam = {
      ...searchParam,
    };
    this.setData({
      isLoading: true,
    });
    api
      .apiStoreCancelList({
        ...searchParam,
        page,
      })
      .then((res) => {
        if (
          JSON.stringify(this.data.searchParam) !== JSON.stringify(searchParam)
        )
          return;
        let data = res.data;
        if (page === 1) {
          searchNum[0].num = Number(data.entirety.cost_sum).toFixed(2);
          searchNum[0].subhead = data.entirety.total_count + "笔";
          searchNum[1].num = Number(data.entirety.card_sum).toFixed(2);
          searchNum[1].subhead = data.entirety.card_count + "笔";
          searchNum[2].num = Number(data.entirety.course_sum).toFixed(2);
          searchNum[2].subhead = data.entirety.course_count + "笔";

          searchNum[3].num = Number(data.entirety.card_week_cost).toFixed(2);
          searchNum[3].subhead = data.entirety.card_week_count + "笔";
          searchNum[4].num = Number(data.entirety.card_month_cost).toFixed(2);
          searchNum[4].subhead = data.entirety.card_month_count + "笔";
          searchNum[5].num = Number(data.entirety.card_season_cost).toFixed(2);
          searchNum[5].subhead = data.entirety.card_season_count + "笔";

          searchNum[6].num = Number(data.entirety.card_year_cost).toFixed(2);
          searchNum[6].subhead = data.entirety.card_year_count + "笔";
          searchNum[7].num = Number(data.entirety.card_exp_cost).toFixed(2);
          searchNum[7].subhead = data.entirety.card_exp_count + "笔";
          searchNum[8].num = Number(data.entirety.card_other_cost).toFixed(2);
          searchNum[8].subhead = data.entirety.card_other_count + "笔";
          this.setData({
            searchNum,
          });
        }
        if (page >= data.all_page) this.setData({ isMore: false });
        if (data.list.length) {
          data.list = data.list.map((i) => {
            i.user_info.tagVal = 0;
            i.user_info.tagStr = "会员";
            i.coach_info.tagVal = 1;
            i.coach_info.tagStr = "教练";
            return { ...i };
          });
          list.push(...data.list);
          this.setData({ list });
        }
      })
      .finally((_) => {
        this.setData({
          isLoading: false,
        });
      });
  },
  copyText(e){
    wx.setClipboardData({
      data: e.currentTarget.dataset.text,
      success: function(res) {
        wx.showToast({
          title: '复制成功',
          icon: 'success'
        })
      },
      fail: function(err) {
        console.error('复制失败:', err)
      }
    })
  },
  confirm(e) {
    let list = JSON.parse(JSON.stringify(this.data.list));
    let id = e.currentTarget.dataset.id;
    wx.showModal({
      title: "确认通过",
      content: `确认通过核销？`,
      confirmColor: "#73F0EA",
      success: (res) => {
        if (res.confirm) {
          api
            .apiStoreAgree({
              id,
            })
            .then((res) => {
              if (res.code === 1) {
                wx.showToast({
                  title: "核销通过成功",
                  icon: "none",
                });
                list[list.findIndex((i) => i.id === id)].audit_status = {
                  desc: "已通过",
                  status: 0,
                };
                this.setData({
                  list
                })
              } else {
                wx.showToast({
                  title: res.msg,
                  icon: "none",
                });
              }
            });
        }
      },
    });
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (!this.data.isMore) return;
    if (this.data.isLoading) return;
    let page = this.data.page;
    this.setData({
      page: page + 1,
    });
    this.getDataList();
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
